#ifndef _HEAD_H
#define _HEAD_H
#include <my_head.h>
#endif

#ifndef _UTILS_H
#define _UTILS_H
#include"./headfile/utils.h"
#endif


#define rootPath "./userSpace/"



bool addEvent(int con_fd , int epo_fd)
{
    epoll_event event;
    event.data.fd = con_fd;
    event.events = EPOLLIN;
    epoll_ctl(epo_fd , EPOLL_CTL_ADD , con_fd , &event);
    return true;
}
int initSocket(char *ip , char* port)
{
    int sofd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in sockaddr;
    sockaddr.sin_addr.s_addr = inet_addr(ip);
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_family = AF_INET;
    bind(sofd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
    listen(sofd, 100);
    return sofd;
}
int clientConnect(char *sourceIP , char *sourcePort)
{
    int socketFd = socket(AF_INET, SOCK_STREAM, 0);
    // 方式一: inet_addr
    // 把 点分十进制, 转成in_addr_t类型(网络IP),  把其存储到结构体in_a   11    //in_addr_t addrTIP = inet_addr(sourceIP);
    //struct in_addr inAddr;
    //inAddr.s_addr = addrTIP;
   
    // 方式二: inet_aton
    struct in_addr inAddr;
    inet_aton(sourceIP, &inAddr);
    // 把端口转为int类型
    int sourcePortInt = atoi(sourcePort);
    // 把端口号: 有主机字节序, 转为网络字节序
    int sourcePortNet = htons(sourcePortInt);

    // 构建"struct sockaddr"类型
    struct sockaddr_in socketAddr;
    socketAddr.sin_family = AF_INET;
    socketAddr.sin_addr = inAddr;
    socketAddr.sin_port = sourcePortNet;

    // 客户端向服务器发起建立连接请求
    int res_connect = connect(socketFd, (struct sockaddr *)&socketAddr , sizeof(socketAddr));
    CHECK_ERROR(res_connect , -1 , "connect has occured fail!\n");

    return socketFd;
}

//传19
void GenerateStr(int STR_LEN , char str[])
 {
    memset(str , 0 , STR_LEN + 1);
    int i,flag;
     
    srand(time(NULL));//通过时间函数设置随机数种子，使得每次运行结果随机。
    for(i = 0; i < STR_LEN; i ++)
    {
        flag = rand()%3;
        switch(flag)
        {
        case 0:
            str[i] = rand()%26 + 'a'; 
            break;
        case 1:
            str[i] = rand()%26 + 'A'; 
            break;
        case 2:
            str[i] = rand()%10 + '0'; 
            break;
        }
    }
    //printf("%s\n", str);//输出生成的随机数。
 }
